package com.lineage.server.serverpackets;

import com.lineage.config.ConfigOther;
import com.lineage.server.datatables.lock.DwarfShopReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1ItemStatus;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.templates.L1ShopS;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class S_CnSShopSellListarmor extends ServerBasePacket {
  private byte[] _byte;
  
  public S_CnSShopSellListarmor(L1PcInstance pc, L1NpcInstance npc, int type) {
    this._byte = null;
    buildPacket(pc, npc.getId(), type);
  }
  
  private void buildPacket(L1PcInstance pc, int tgObjid, int type) {
    Map<L1ShopS, L1ItemInstance> shopItems = new HashMap<>();
    Map<Integer, L1ItemInstance> srcMap = DwarfShopReading.get().allItems();
    Iterator<Integer> iterator = srcMap.keySet().iterator();
    while (iterator.hasNext()) {
      Integer key = iterator.next();
      L1ShopS info = DwarfShopReading.get().getShopS(key.intValue());
      if (info == null)
        continue; 
      if (info.get_end() != 0)
        continue; 
      if (info.get_item() == null)
        continue; 
      L1ItemInstance item = srcMap.get(key);
      switch (type) {
        case 2:
          if (item.getItem().getUseType() != 2)
            continue; 
          break;
        case 18:
          if (item.getItem().getUseType() != 18)
            continue; 
          break;
        case 19:
          if (item.getItem().getUseType() != 19)
            continue; 
          break;
        case 20:
          if (item.getItem().getUseType() != 20)
            continue; 
          break;
        case 21:
          if (item.getItem().getUseType() != 21)
            continue; 
          break;
        case 22:
          if (item.getItem().getUseType() != 22)
            continue; 
          break;
        case 25:
          if (item.getItem().getUseType() != 25)
            continue; 
          break;
        case 37:
          if (item.getItem().getUseType() != 37)
            continue; 
          break;
        case 23:
          if (item.getItem().getUseType() != 23)
            continue; 
          break;
        case 40:
          if (item.getItem().getUseType() != 40)
            continue; 
          break;
        case 24:
          if (item.getItem().getUseType() != 24)
            continue; 
          break;
        case 50:
          if (item.getItem().getUseType() == 2)
            continue; 
          if (item.getItem().getUseType() == 18)
            continue; 
          if (item.getItem().getUseType() == 19)
            continue; 
          if (item.getItem().getUseType() == 20)
            continue; 
          if (item.getItem().getUseType() == 21)
            continue; 
          if (item.getItem().getUseType() == 22)
            continue; 
          if (item.getItem().getUseType() == 25)
            continue; 
          if (item.getItem().getUseType() == 37)
            continue; 
          if (item.getItem().getUseType() == 23)
            continue; 
          if (item.getItem().getUseType() == 24)
            continue; 
          if (item.getItem().getUseType() == 40)
            continue; 
          if (item.getItem().getUseType() == 1)
            continue; 
          break;
      } 
      shopItems.put(info, item);
    } 
    writeC(70);
    writeD(tgObjid);
    if (shopItems.size() <= 0) {
      writeH(0);
      return;
    } 
    writeH(shopItems.size());
    int i = 0;
    Iterator<L1ShopS> iterator2 = shopItems.keySet().iterator();
    while (iterator2.hasNext()) {
      L1ShopS key2 = iterator2.next();
      i++;
      L1ItemInstance item = shopItems.get(key2);
      pc.get_otherList().add_cnSList(item, i);
      writeD(i);
      writeH(item.getItem().getGfxId());
      writeD(key2.get_adena());
      if (ConfigOther.SHOPINFO) {
        L1ItemStatus itemInfo = new L1ItemStatus(item);
        byte[] status = itemInfo.getStatusBytes(true).getBytes();
        writeC(status.length);
        byte[] array;
        int length = (array = status).length;
        int j = 0;
        while (j < length) {
          byte b = array[j];
          writeC(b);
          j++;
        } 
        continue;
      } 
      writeC(0);
    } 
    writeH(6100);
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
